// subPages/communityService/homemakingServiceItem/homemakingServiceItem.js
const app = getApp();
var utils = require('../../../utils/util');
var loadMoreView,p = 1,num = 10,type='service'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      selectIndex:0,
      array:["好评优先","距离优先","人气优先",""],
      homemakingServiceList:[

      ]
  },
    makeCall(e){
        let phone = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber:phone
        })
    },
    goToDetails:function(e){
        let id = e.currentTarget.dataset.id;
        let type = this.data.type,url;
        if(type == 0){
            url = '/subPages/communityService/homemakingDetails/homemakingDetails?id='+id
        }else if(type == 1){
            url = '/subPages/communityService/healthServiceDetails/healthServiceDetails?id='+id
        }
        wx.navigateTo({
            url:url
        })
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that = this;
      var id = options.id;
      that.setData({
          classId:id,
          type:options.type
      })
      loadMoreView = this.selectComponent("#loadMoreView")
      this.setTitleFun(id);
      this.getHomemakingService();
  },
    getHomemakingService(){
      var that = this;
      let type = that.data.type,url;
      if(type == 0){
          url = '/get_community_service_home_shop'
      }else if(type == 1){
          url = '/get_community_service_up_home_shop'
      }
      utils.getAjax(`${app.globalData.apiUrl}${url}`,function(res){
          // console.log(res);
          if(res.data.list.length != 0){
              var list = [];
            if(p == 1){
                list = res.data.list
            }else{
                list = that.data.homemakingServiceList.concat(res.data.list)
            }
            that.setData({
                homemakingServiceList:list
            })
          }else{
            that.setData({
                homemakingServiceList:[]
            })
          }
          loadMoreView.loadMoreComplete(res.data,type)
      },{
          class_id:that.data.classId,
          p:p,
          num:num,
          community_id:app.globalData.communityId
      })
    },
    //获取服务数据异常
    shopDataErr(err){
        if(p>1){
            loadMoreView.loadMoreFail()
        }else{
            util.showModal('提示','获取商品失败，请向上滑动屏幕')
            this.setData({
                isFail: true
            })
        }
    },
    loadMoreListener: function(e) {
        p += 1
        this.getHomemakingService()
    },

    clickLoadMore: function(e) {
        this.getHomemakingService()
    },
  setTitleFun(id){
      var text = '';
      let _this = this;
      if(_this.data.type == 0){
          switch (id) {
              case '1':text ='保洁清洁'
                  break;
              case '2':text = '衣物洗护'
                  break;
              case '3':text = '个人卫生'
                  break;
              case '4':text = '上门开锁'
                  break;
              case '5':text = '维修服务'
                  break;
              default: text = '家政服务'
          }
      }else if(_this.data.type == 1){
          switch (id) {
              case '1':text ='足疗按摩'
                  break;
              case '2':text = '洗浴/汗蒸'
                  break;
              case '3':text = '口腔牙科'
                  break;
              case '4':text = '体验中心'
                  break;
              case '5':text = '医学美容'
                  break;
               default: text = '健康服务'
          }
      }
      utils.setNavBarTitle(text)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.isFail){
          this.getHomemakingService()
          return
      }
      loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
